import {md5} from 'js-md5';
import router from "@/route/index.js";
import {getCurrentCompanyName} from "@/composition/useRouterLink.js";

// 去除对象的null或者undefined属性
export const clearObject = (obj) => {
  for (const key in obj) {
    if (obj[key] === undefined || obj[key] === null) {
      delete obj[key];
    }
  }
  return obj;
};

// 获取32位Md5
export const getMd5 = (str) => {
  return md5(str);
};

// 验证邮箱是否合法
export const validateEmail = (email) => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
};

// 验证手机号是否合法
export const validatePhone = (phone) => {
  const phoneRegex = /^1[3-9]\d{9}$/;
  return phoneRegex.test(phone);
};

// 找好返回用户名和邮箱
export const explainAccount = (account) => {
  let username;
  let email;
  let mobile;
  let isEmail = validateEmail(account);
  let isMobile = validatePhone(account);
  if (isEmail) {
    return {username, email: account, mobile};
  }
  if (isMobile) {
    return {username, email, mobile: account};
  }
  return {username: account, email, mobile};
};

// 验证密码强度 最少需要数字和大小写字母同时存在
export const validatePassword = (password) => {
  // 正则表达式，匹配包含数字和大小写字母的密码，最少长度为6位
  let regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$/;
  if (regex.test(password)) {
    return true;
  } else {
    return false;
  }
};

// 弹窗提示
export const elTip = (message, type = "success") => {
  ElMessage[type](message);
};

// 路由跳转到指定name的路由
export const routerTo = (name, index) => {
  if (!name) {
    return;
  }
  let curCompanyName = getCurrentCompanyName();
  router.push({
    name, params: {
      companyName: curCompanyName
    }
  });
};

// 刷新当前组件
export const refreshComponent = () => {
  router.go(0);
};

// 获取svg文件路径
export const getSvgPath = (name) => {
  return new URL(`../assets/svg/${name}.svg`, import.meta.url).href;
};

// 处理名称为6位之内
export const handleName = (name) => {
  if (name.length > 6) {
    return name.slice(0, 3) + "..." + name.slice(-3);
  }
  return name;
};

// 获取今天0点和昨天0点的微秒时间戳 考虑跨月的时间点
export const getTodayAndYesterdayZeroTime = () => {
  let now = new Date();
  let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  return {
    today: today.getTime(),
    yesterday: today.getTime() - 1000 * 60 * 60 * 24
  };
};

// 获取今天0点和本月1号0点的微秒时间戳
export const getTodayAndMonthZeroTime = () => {
  let now = new Date();
  let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  let month = new Date(now.getFullYear(), now.getMonth(), 1);
  return {
    today: today.getTime(),
    month: month.getTime()
  };
};

// 导出csv
export const exportCsv = (header, data, fileName) => {
  const content = [header.map(it => it.title).join(","), ...data.map(item => header.map(it => `"${item[it.key]}"`).join(","))];
  const blob = new Blob([content.join("\r\n")], { type: "text/csv;charset=utf-8;" });
  if (navigator.msSaveBlob) {
    navigator.msSaveBlob(blob, fileName);
  } else {
    const link = document.createElement("a");
    if (link.download !== undefined) {
      const url = URL.createObjectURL(blob);
      link.setAttribute("href", url);
      link.setAttribute("download", fileName);
      link.style.visibility = "hidden";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      elTip("导出成功");
    }
  }
};
